package com.sun.alen.controller;


import com.sun.alen.entity.PageBean;
import com.sun.alen.entity.Profession;
import com.sun.alen.entity.Result;
import com.sun.alen.service.ProfessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/profession")
@Slf4j
public class ProfessionController {
    @Autowired
    private ProfessionService professionService;


    /**
     * 获取所有专业
     * @return
     */
    @GetMapping("/all")
    public Result all(){
        List<Profession> professions=professionService.all();
        return Result.success(professions);
    }


    /**
     * 分页获取专业
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result list(@RequestParam(name = "page", required = false, defaultValue = "1")Integer currentPage,
                       @RequestParam(name = "size", required = false, defaultValue = "5")Integer pageSize){
        PageBean pageBean=professionService.list(currentPage,pageSize);
        return Result.success(pageBean);
    }

    /**
     * 添加专业
     * @param profession
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Profession profession){
        boolean flag=professionService.add(profession);
        if (flag){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改专业
     * @param profession
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Profession profession){
        boolean flag=professionService.update(profession);
        if (flag){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 根据ID删除专业
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(String id){
        boolean flag=professionService.delete(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 搜索
     * @param input
     * @param select
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestParam(name = "input",required = false,defaultValue = "")String input,
                         @RequestParam(name = "select",required = false,defaultValue = "1")String select,
                         @RequestParam(name = "page",required = false,defaultValue = "1")Integer currentPage,
                         @RequestParam(name = "size",required = false,defaultValue = "5")Integer pageSize){
        PageBean pageBean=professionService.search(input,select,currentPage,pageSize);
        return Result.success(pageBean);
    }
}
